package com.example.testnetty.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.blankj.utilcode.util.SPUtils;
import com.example.testnetty.R;
import com.example.testnetty.bean.LocationBean;
import com.example.testnetty.netty.NettyClientRtk;
import com.example.testnetty.utlis.JSONUtils;
import com.example.testnetty.utlis.JSonUtlis;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;

public class MainActivity extends AppCompatActivity {


    private final String TAG = MainActivity.class.getSimpleName();


    private int count=0;
    private String s1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        s1 = JSonUtlis.getJson("lnglat.json", MainActivity.this);
        SPUtils.getInstance().put("lnglat",s1);
        NettyClientRtk nettyClientRtk=new NettyClientRtk();
        nettyClientRtk.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //撤销定时器

    }

    public static class RtkClientHandler extends SimpleChannelInboundHandler<byte[]> {

        //连接成功触发channelActive
        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {

            Log.i("TAG", "channelActive: "+111);
        }
        //断开连接触发channelInactive
        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            // TODO: 2018/6/13 重连操作
            Log.i("TAG", "channelActive: "+222222);
        }


        //异常回调,默认的exceptionCaught只会打出日志，不会关掉channel
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
            Log.i("TAG", "channelActive: "+99999);
            cause.printStackTrace();
            ctx.close();
        }
        private int miao=0;
        //客户端收到消息
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, byte[] msg) throws Exception {
            //通过接口将值传出去
            Log.i("TAG", "channelRead1: "+new String(msg));
            String str = new String(msg);
            Pattern p = Pattern.compile("\\d+");
            Matcher m = p.matcher(str);

            m.find();
            class MyTimerTask extends TimerTask {
                public void run() {

                    String lnglat = SPUtils.getInstance().getString("lnglat");
                    ArrayList<LocationBean> locationBean = JSONUtils.fromJsonList(lnglat, LocationBean.class);
                    if (miao<locationBean.size()){
                        Log.i("TAG", "channelRead0: "+444444);
                        ctx.channel().writeAndFlush(locationBean.get(miao).toString().getBytes());
                        miao++;
                    }else {

                    }
                }
            }
            if ("200".equals(m.group())){
                Timer timer = new Timer();
                timer.schedule(new MyTimerTask(), 0, 1000);

            }

        }
        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            Log.i("TAG", "channelActive: "+99666);
            if (evt instanceof IdleStateEvent) {
                IdleStateEvent e = (IdleStateEvent) evt;
                switch (e.state()) {
                    case WRITER_IDLE:
                        System.err.println("writer_idle over.");
                        break;
                    case READER_IDLE:
                        System.err.println("reader_idle over.");
                        break;
                    case ALL_IDLE:
                        System.err.println("all_idle over.");
                    default:
                        break;
                }
            }}

    }

}